
package hutool.cron;

import cn.hutool.core.date.DateUtil;
import cn.hutool.cron.CronUtil;
import cn.hutool.cron.Scheduler;
import cn.hutool.cron.TaskExecutor;
import cn.hutool.cron.listener.SimpleTaskListener;
import cn.hutool.cron.listener.TaskListener;
import cn.hutool.cron.task.Task;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 黄乾坤
 * @create 2023-09-14 18:56
 */
@Slf4j
public class Cron {

    private static volatile boolean success = false;

    public static void main(String[] args) {

        CronUtil.schedule("ID1", "0/10 * * * * ? *", () -> {
            System.out.println(DateUtil.date() + ":" + Thread.currentThread().getId() + ":" + "ID1");
            System.out.println(success);
            if (success) {
                CronUtil.remove("ID1");
            }
        });

        CronUtil.schedule("ID2", "0/20 * * * * ? *", () -> {
            System.out.println("2222222222");
            throw new RuntimeException("1111");
        });

        CronUtil.schedule("ID3", "0/30 * * * * ? *", () -> {
            System.out.println("333333333");
            success = true;
            System.out.println(success);
        });

        CronUtil.start(true);

        synchronized (Cron.class) {
            while (true) {
                try {
                    Cron.class.wait();
                } catch (InterruptedException e) {
                    log.error("synchronize main function error", e);
                }
            }
        }

    }
}
